<?php
    /**
     * Created by PhpStorm.
     * User: jackhhy
     * Date: 2019/7/4-16:39
     * Link: https://gitee.com/luckygyl/LuckyAdmin
     * FileName: Upload.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     */

    namespace app\admin\controller;


    use service\UtilService;
    use think\Request;

    class Upload extends SystemBase
    {
        protected $upload;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
            $this->upload=new \app\admin\model\Upload();
        }



        /**
         * @return mixed
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: index
         * @describe:素材列表
         */
        public function index(){

            $param = $this->request->param();

            $res   = $this->upload->GetSucaiDataPage($param);

            $this->assign('data', $res->toArray());

            $this->assign('page', $res->render());
            $request = Request::instance();
            $domain  = $request->domain();
            $this->assign("domain", $domain);

            return $this->fetch();
        }


        /**
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: del
         * @describe:删除
         */
        public function del(){
            if($this->request->isPost()){
                $ids=$this->request->post("ids");
                $res=$this->upload->del($ids);
                if($res){
                    $ars = ['describe' => '删除系统附件', 'type' => 2, 'admin_name' => self::$admin_info['username'], 'browse' => UtilService::getBrowser(), 'model' => self::$mode_name, 'controller' => self::$controller_name, 'action' => self::$action_name];
                    AddLogs($ars); //添加操作日志
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
            }
        }

        /**
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: Jackhhy <jackhhy520@qq.com>
         * @name: down_img
         * @describe:下载文件
         */
        public function down_img(){
            $id=input("id"); //文件路径
            $file=db("upload")->find($id);
            if (!$file){
                $this->error("文件ID不存在");exit;
            }
            $file_name=$file['filename'];
            $file_path=ROOT_PATH.'public'.$file['file_path'];

            if(!file_exists($file_path)){
                $this->error("文件不存在");exit;
            }

            $file1=fopen($file_path,"r");
            Header("Content-type: application/octet-stream;charset=utf-8");
            Header("Accept-Ranges: bytes");
            header("Content-Length: " . filesize($file_path));//文件大小
            header('Content-Disposition:attachment;filename='.$file_name.'');
            ob_clean();
            flush();
            echo fread($file1,filesize($file_path)); //输出文件大小到浏览器
            fclose($file1);
            exit();
        }


    }